home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / dosunpk.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  3KB  |  145 lines

  1. /* (C) Copyright 1993 by John G. Myers
  2.  * All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without
  6.  * fee, provided that the above copyright notice appear in all copies
  7.  * and that both that copyright notice and this permission notice
  8.  * appear in supporting documentation, and that the name of John G.
  9.  * Myers not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  John G. Myers makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as
  13.  * is" without express or implied warranty.
  14.  *
  15.  * JOHN G. MYERS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL JOHN G. MYERS BE LIABLE FOR ANY SPECIAL,
  18.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #include <stdio.h>
  24. #include <dos.h>
  25. #include <errno.h>
  26. #include "version.h"
  27.  
  28. extern int optind;
  29. extern char *optarg;
  30.  
  31. extern int overwrite_files;
  32. extern int didchat;
  33. int quiet;
  34.  
  35. main(argc, argv)
  36. int argc;
  37. char **argv;
  38. {
  39.     int opt;
  40.     FILE *file;
  41.     char *p, *q, *path = 0;
  42.     int extractText = 0;
  43.     int done, gotone;
  44.     struct _find_t ffblk;
  45.     
  46.     while ((opt = getopt(argc, argv, "qftC:")) != EOF) {
  47.     switch (opt) {
  48.     case 'f':
  49.         overwrite_files = 1;
  50.         break;
  51.  
  52.     case 'q':
  53.         quiet = 1;
  54.         break;
  55.  
  56.     case 't':
  57.         extractText = 1;
  58.         break;
  59.  
  60.     case 'C':
  61.         if (chdir(optarg)) {
  62.         perror(optarg);
  63.         exit(1);
  64.         }
  65.         break;
  66.  
  67.     default:
  68.         usage();
  69.     }
  70.     }
  71.  
  72.     if (optind == argc) {
  73.     fprintf(stderr, "munpack: reading from standard input\n");
  74.     didchat = 0;
  75.     handleMessage(stdin, "text/plain", 0, extractText, 0);
  76.     if (!didchat) {
  77.         fprintf(stdout,
  78.             "Did not find anything to unpack from standard input\n");
  79.     }
  80.     exit(0);
  81.     }
  82.  
  83.     while (argv[optind]) {
  84.     if (path) free(path);
  85.     path = 0;
  86.  
  87.     p = argv[optind];
  88.     if (p[1] == ':') p += 2;
  89.     if (q = strrchr(p, '\\')) p = q+1;
  90.     if (q = strrchr(p, '/')) p = q+1;
  91.  
  92.     if (p != argv[optind]) {
  93.         path = xmalloc(strlen(argv[optind])+20);
  94.         strcpy(path, argv[optind]);
  95.         p = path + (p - argv[optind]);
  96.     }
  97.  
  98.     gotone = 0;
  99.     done = _dos_findfirst(argv[optind], 0, &ffblk);
  100.     while (!done) {
  101.         if (path) strcpy(p, ffblk.name);
  102.         file = fopen(path ? path : ffblk.name, "r");
  103.         if (!file) {
  104.         perror(path ? path : ffblk.name);
  105.         }
  106.         else {
  107.         didchat = 0;
  108.         handleMessage(file, "text/plain", 0, extractText, 0);
  109.         fclose(file);
  110.         if (!didchat) {
  111.             fprintf(stdout, 
  112.                 "Did not find anything to unpack from %s\n",
  113.                 argv[optind]);
  114.         }
  115.         }
  116.         gotone = 1;
  117.         done = _dos_findnext(&ffblk);
  118.     }
  119.     if (!gotone) {
  120.         perror(argv[optind]);
  121.     }
  122.     optind++;
  123.     }
  124.     exit(0);
  125. }
  126.  
  127. usage() {
  128.     fprintf(stderr, "usage: munpack [-f] [-q] [-C directory] [files...]\n");
  129.     exit(1);
  130. }
  131.  
  132. warn(s)
  133. char *s;
  134. {
  135.     fprintf(stderr, "munpack version %s\n", MPACK_VERSION);
  136.     fprintf(stderr, "munpack: warning: %s\n", s);
  137. }
  138.  
  139. chat(s)
  140. char *s;
  141. {
  142.     didchat = 1;
  143.     if (!quiet) fprintf(stdout, "%s\n", s);
  144. }
  145.